
$(function() {
	$("input[id^='code_s']:visible").autocomplete(Test.customer_my_code);

	$("select[id^='continent_s']:visible").selectChain({
		target: $("select[id^='country_s']:visible"),
		key: "code",
		value: "name",
		url: 'info.php?do=Country.ajax_continent',
		data: { ajax: true }
	}).trigger('change');

	Tip.emp('业务员');
});

var validator;

var CustomerMy = {
	
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.code = form.code.value;
				body.code_t = form.code_t.value;
				body.name = form.name.value;
				body.fullname = form.fullname.value;
				body.cert_type = form.cert_type.value;
				body.cert_no = form.cert_no.value;
				
				body.continent = form.continent.value;
				body.country = form.country.value;

				body.address = form.address.value;
				body.url = form.url.value;
				body.jj = form.jj.value;
				
				$(this).dialog("close");

				$.post('contact.php?do=CustomerMy.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},	

	popup: function (e, p2, id, code, status) {
		$.cookie('id_', id);
		
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'contact.php?do=CustomerMy.edit&id=' + id;	
		var c2 = 'contact.php?do=CustomerMy.view&id=' + id;
		var c3 = 'contact.php?do=CustomerMy.bj&id=' + id + p2;
		var c4 = 'contact.php?do=CustomerMy.bank&id=' + id;

		var c6 = 'customer.php?do=QuoteMy.add&id=' + id;
		var c7 = 'mold.php?do=MoldMy.direct&id=' + id;

		var a1 = ['客户编辑', 'url', 'CustomerMy.dialog_edit_', '650x510'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		var a3 = ['指定报价', 'url', 'CustomerMy.dialog_bj', '600x360'];	
		
		var a4 = ['银行帐号', 'url', 'CustomerMy.dialog_bank', '650x450'];
		
		var a6 = ['添加报价', 'url', 'CustomerMy.dialog_quote', '580x360'];		
		var a7 = ['添加产品', 'url', 'CustomerMy.dialog_mold', '580x360'];
		
		var c8 = 'contact.php?do=CustomerMy.upload&id=' + id;
		var a8 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var s = c1 + ", " + c2 + ", " + c3 + ", " + c4 + ", " + c6 + ", " + c7 + ", " + c8;

		return popup(e, [a1, a2, a3, a4, a6, a7, a8], s);
	},	

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				if (!ContactEdit.validate_edit()) {
					return false;
				}

				var id = $("input[name='id']");
				var linkman = $("input[name='linkman']");
				var job = $("input[name='job']");
				var dept = $("input[name='dept']");
				
				var tel = $("input[name='tel']");
				var fax = $("input[name='fax']");
				
				var mobile = $("input[name='mobile']");
				var email = $("input[name='email']");
				var address = $("input[name='address']");
				var geo = $("input[name='geo']");
				
				var qq = $("input[name='qq']");
				var skype = $("input[name='skype']");
				var content = $("input[name='content']");

				var aa = new Array();
				for (var i = 0; i < linkman.length; i++) {
					var contact = {};
					var b = $("#form" + z[i] + " :input[name='id']");
					if (b != null) {
						contact.id = $("#form" + z[i] + " :input[name='id']").val();
					}

					contact.linkman = $(linkman[i]).val();
					contact.job = $(job[i]).val();
					contact.dept = $(dept[i]).val();
					
					contact.tel = $(tel[i]).val();
					contact.fax = $(fax[i]).val();
					
					contact.mobile = $(mobile[i]).val();
					contact.email = $(email[i]).val();
					contact.address = $(address[i]).val();
					contact.geo = $(geo[i]).val();
					
					contact.qq = $(qq[i]).val();
					contact.skype = $(skype[i]).val();
					contact.content = $(content[i]).val();
					
					aa.push(contact);
				}

				var form = $("#form_edit")[0];
				
				var star = $("input[name='star']:checked");

				var body = { };		
				
				body.id = form.info_id.value;
				body.code = form.code.value;
				body.name = form.name.value;
				body.fullname = form.fullname.value;
				body.cert_type = form.cert_type.value;
				body.cert_no = form.cert_no.value;
				
				body.star = star.val();

				body.continent = form.continent.value;
				body.country = form.country.value;

				body.address = form.address.value;
				body.geo = form.main_geo.value;
				body.url = form.url.value;
				
				body.jj = form.jj.value;
				body.status = form.status.value;

				body.contact = $.toJSON(aa);
				
				body.z = $.cookie('contact');				
				$.cookie('contact', null);
				
				$(this).dialog("close");

				var url = 'contact.php?do=CustomerMy.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_bj: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"指定": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.bj = form.bj.value;

				$(this).dialog("close");

				var url = 'contact.php?do=CustomerMy.bjPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	dialog_bank: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {			
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
							
				var form = $("#form_add")[0];
	
				var body = { };
				body.id = form.id.value;
				
				body.category = form.category.value;
				body.bank = form.bank.value;
				body.branch = form.branch.value;
	
				body.province = form.province.value;
				body.city = form.city.value;
				
				body.zone = form.zone.value;
				body.type = form.type.value;
				
				body.swift = form.swift.value;
				body.name = form.name.value;
				body.code = form.code.value;
				
				body.content = form.content.value;
				
				var url = 'contact.php?do=CustomerMy.ajax_bank';
				$.getJSON(url, body, function(json) {
					if (json.status == '1') {
						$.post('contact.php?do=CustomerMy.bankPost', body, function(data) {
							$('#dialog_add').html(data);
						});
					} else {
						// alert($.toJSON(json));
						alert("帐号信息已存在，请联系管理员！");
					}					
				});
			}
		});
	
		dialog_add(url, title, xy);
	},

	dialog_quote: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"报价申请": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];
				
				if (!CustomerMy.project_name(form.code.value)) {
					alert("项目编号不符合规范，请重新命名！");
					return false;
				}

				var body = { };
				body.id = form.id.value;
				body.code = form.code.value;
				body.num = form.num.value;
				
				body.category = form.category.value;
				body.urgent = form.urgent.value;
				body.date = form.date.value;
				body.content = form.content.value;

				$(this).dialog("close");

				var url = 'customer.php?do=QuoteMy.addPost';

				$.post(url, body, function(data) {
					var json = $.evalJSON(data);
					
					if (json.status == 1) {
						alert("添加成功！");
					} else {
						alert("项目编号已存在，添加失败，请重新添加！");
					}				
				});
			}
		});

		dialog_add(url, title, xy);
	},

	project_name: function (s) {
		if (s == null || s.trim() == '') {
			return true;
		}

		var value = s.trim();
		var regex;

		regex = /^(GM|TM|T)(\d+)\.(\d+)$/
		if (regex.test(value)) {
			return true;
		}

		return false;
	},
	
	dialog_mold: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.id = form.id.value;
				body.code = form.code.value;
				body.category = form.category.value;
				body.date = form.date.value;
				body.content = form.content.value;

				$(this).dialog("close");

				var url = 'mold.php?do=MoldMy.directPost';

				$.post(url, body, function(data) {
					alert("提交成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	download: function (key) {
		
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'contact.php?do=CustomerMy.download&key=' + key;
		
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'contact.php?do=CustomerMy.upload&id=' + id;
		
		var url = 'contact.php?do=CustomerMy.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	}
};
